// pages/product/SpellGroupDetails.js
var Zan = require('../../dist/index');
var WxParse = require('../../wxParse/wxParse.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
function countdown(that) {
  for (var i = 0; i < that.data.groupList.length; i++) {
    var EndTime = that.data.groupList[i].endDate || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.data.groupList[i].clock = dateformat(total_micro_second)
    // that.setData({
    //   clock: dateformat(total_micro_second)
    // });
    if (total_micro_second <= 0) {
      that.data.groupList[i].clock = "已经截止"
      //return;
    }
  }
  that.setData({
    groupList: that.data.groupList
  })
  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }
    , 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  let date = '';
  if (day == 0) {
    date = hr + ":" + min + ":" + sec;
  } else {
    date = day + ":" + hr + ":" + min + ":" + sec;
  } 
  return date;
}
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    shared: true,
    product: {},
    listSpec: [],
    showDialog: false,
    count: 1,
    radioCheckVal01: '',
    radioCheckVal02: '',
    radioCheckVal03: '',
    intelvalue01: 0,
    intelvalue02: 0,
    intelvalue03: 0,
    isClickSpecification1: 0,
    isClickSpecification2: 0,
    isClickSpecification3: 0,
    item01: '',
    item02: '',
    item03: '',
    listspecid: '',
    nowclock: '',
    selectedSpec: null,
    specName: '',
    RealListspec: {},
    price: 0,
    distributorId: "",
    spellGroup: null,
    spellGroupId: '',
    userSpellGroupId: '',
    mobile: null,
    groupCount: 0,
    formId: '',
    canIUseMobile: wx.canIUse('button.open-type.getPhoneNumber')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    if (app.globalData.userId) {
      bind(options);
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        bind(options);
      }
    }

    wx.showShareMenu({
      withShareTicket: true //要求小程序返回分享目标信息
    })
    /**
     * 获取个人及商家信息
     */
    if (app.globalData.userAndTenantInfo) {
      this.setData({
        mobile: app.globalData.userAndTenantInfo.mobile
      })
    }

    wx.getUserInfo({
      success: res => {
        this.setData({
          nickName: res.userInfo.nickName,
        })
      }
    })
    if (options.userSpellGroupId) {
      //存在userSpellGroupId表示参团
      this.setData({
        userSpellGroupId: options.userSpellGroupId,

      })
    }
    this.setData({
      spellGroupId: options.spellGroupId,
      userid: app.globalData.userId
    })

    //network._post_json(this, api.getProductDetails, { productId: options.productId }, detailsSuccess);
    //network._post_json(this, api.groupRule, { spellgroupId: this.data.spellGroupId }, ruleSuccess);
    network._post_json(this, api.SpellGroupDetail, { spellGroupsId: this.data.spellGroupId }, detailsSuccess);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.SpellGroupDetail, { spellGroupsId: this.data.spellGroupId }, detailsSuccess);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let price = 0;
    let pricet2 = 0;
    if (this.data.userSpellGroupId) {
      price = this.data.proprice;
      pricet2 = this.data.orderprice;
    } else {
      if (this.data.listspecid === '') {
        price = this.data.RealListspec.specValue1;
        pricet2 = this.data.product.sgPrice;
      } else {
        price = this.data.product.proPrice
        pricet2 = this.data.RealListspec.specValue2;
      }
    }
    return {
      title: '花' + price2 / 100 + '元，就能拼得原价' + price / 100 + '元的【' + this.data.product.title + '】，太超值了',
      path: '/pages/product/SpellGroupDetails?shared=' + app.globalData.userId + '&productId=' + this.data.product.id + '&spellGroupId=' + this.data.spellGroup.id,
      // imageUrl: this.data.saleInfo.qrCode,
      success: function (res) {
        // 转发成功
        if (res.shareTickets) {
          wx.getShareInfo({
            shareTicket: res.shareTickets,
            success: function (res) {
              network._post_json(this, api.updateFxcs, { directShareNums: 0, directShareQunNums: 1 }, fxcsSuccess);
            },
            fail: function (res) {
              // 转发失败
              network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
            }
          })
        } else {
          network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
        }

      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  /**
   * 弹出框开关
   */
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog,
      count: 1,
    })
    allPrice(this, this.data.product, this.data.listspecid);
  },


  /* *****************************选择规格***************************** */
  radioCheckedChange: function (e) {
    var Type = e.currentTarget.dataset.type
    var index = e.currentTarget.dataset.index;
    var listspec = {};
    if (Type == 1) {
      var item01 = e.currentTarget.dataset.itema;
      this.setData({
        item01: item01
      })
      this.setData({
        intelvalue02: 1,
        intelvalue03: 1,
        isClickSpecification1: 1
      })
      if (this.data.product.specType2) {
        listspec[this.data.product.specType2] = [];
        for (var i = 0; i < this.data.listSpec.length; i++) {
          if (this.data.listSpec[i].specDetails1 == item01) {
            listspec[this.data.product.specType2].push(this.data.listSpec[i].specDetails2);
            listspec[this.data.product.specType2] = utils.unique2(listspec[this.data.product.specType2]);
            this.setData({
              listspec2: listspec
            })
          }
        }
        if (this.data.radioCheckVal02 > this.data.listspec2[this.data.product.specType2].length) {
          this.setData({
            radioCheckVal02: 0
          })
        }
      }
      if (this.data.product.specType3) {
        listspec[this.data.product.specType3] = [];
        for (var i = 0; i < this.data.listSpec.length; i++) {
          if (this.data.listSpec[i].specDetails1 == item01) {
            listspec[this.data.product.specType3].push(this.data.listSpec[i].specDetails3);
            listspec[this.data.product.specType3] = utils.unique2(listspec[this.data.product.specType3]);
            this.setData({
              listspec3: listspec
            })
          }
        }
      }

      this.setData({
        radioCheckVal01: index
      })

    } else if (Type == 2) {
      var item02 = e.currentTarget.dataset.itemb;
      this.setData({
        item02: item02
      })
      this.setData({
        intelvalue01: 1,
        intelvalue03: 1,
        isClickSpecification2: 1
      })
      listspec[this.data.product.specType1] = [];
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails2 == item02) {
          listspec[this.data.product.specType1].push(this.data.listSpec[i].specDetails1);
          listspec[this.data.product.specType1] = utils.unique2(listspec[this.data.product.specType1]);
          this.setData({
            listspec1: listspec
          })
        }
      }
      if (this.data.product.specType3) {
        listspec[this.data.product.specType3] = [];
        for (var i = 0; i < this.data.listSpec.length; i++) {
          if (this.data.listSpec[i].specDetails2 == item02) {
            listspec[this.data.product.specType3].push(this.data.listSpec[i].specDetails3);
            listspec[this.data.product.specType3] = utils.unique2(listspec[this.data.product.specType3]);
            this.setData({
              listspec3: listspec
            })
          }
        }
      }
      if (this.data.radioCheckVal01 > this.data.listspec1[this.data.product.specType1].length) {
        this.setData({
          radioCheckVal01: 0
        })
      }
      this.setData({
        radioCheckVal02: index
      })
    } else {
      var item03 = e.currentTarget.dataset.itemc;
      this.setData({
        item03: item03
      })
      this.setData({
        intelvalue01: 1,
        intelvalue02: 1,
        isClickSpecification3: 1
      })
      listspec[this.data.product.specType1] = [];
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails3 == item03) {
          listspec[this.data.product.specType1].push(this.data.listSpec[i].specDetails1);
          listspec[this.data.product.specType1] = utils.unique2(listspec[this.data.product.specType1]);
          this.setData({
            listspec1: listspec
          })
        }
      }
      listspec[this.data.product.specType2] = [];
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails3 == item03) {
          listspec[this.data.product.specType2].push(this.data.listSpec[i].specDetails2);
          listspec[this.data.product.specType2] = utils.unique2(listspec[this.data.product.specType2]);
          this.setData({
            listspec2: listspec
          })
        }
      }
      this.setData({
        radioCheckVal03: index
      })
    }

    /**  有规格没有点击  */
    if (this.data.product.specType1 && this.data.product.specType2 && this.data.product.specType3) {
      //点击了两个规格，有一个规格没有点击
      if (!this.data.item01 && this.data.item02 && this.data.item03) {
        this.setData({
          item01: this.data.listspec1[this.data.product.specType1][0]
        })
      } else if (this.data.item01 && !this.data.item02 && this.data.item03) {
        this.setData({
          item02: this.data.listspec2[this.data.product.specType2][0]
        })

      } else if (this.data.item01 && this.data.item02 && !this.data.item03) {
        this.setData({
          item03: this.data.listspec3[this.data.product.specType3][0]
        })

      }
      //点击了1个规格，有2个规格没有点击
      else if (!this.data.item01 && !this.data.item02 && this.data.item03) {
        this.setData({
          item01: this.data.listspec1[this.data.product.specType1][0],
          item02: this.data.listspec2[this.data.product.specType2][0]
        })

      } else if (!this.data.item01 && this.data.item02 && !this.data.item03) {
        this.setData({
          item01: this.data.listspec1[this.data.product.specType1][0],
          item03: this.data.listspec3[this.data.product.specType3][0]
        })
      } else if (this.data.item01 && !this.data.item02 && !this.data.item03) {
        this.setData({
          item02: this.data.listspec2[this.data.product.specType2][0],
          item03: this.data.listspec3[this.data.product.specType3][0]
        })
      }
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails1 == this.data.item01 && this.data.listSpec[i].specDetails2 == this.data.item02 && this.data.listSpec[i].specDetails3 == this.data.item03) {
          this.setData({
            listspecid: i
          })
          break;
        }
      }
    } else if (this.data.product.specType1 && this.data.product.specType2 && !this.data.product.specType3) {
      if (this.data.item01 && !this.data.item02) {
        this.setData({
          item02: this.data.listspec2[this.data.product.specType2][0]
        })
      } else if (!this.data.item01 && this.data.item02) {
        this.setData({
          item01: this.data.listspec1[this.data.product.specType1][0]
        })

      }
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails1 == this.data.item01 && this.data.listSpec[i].specDetails2 == this.data.item02) {
          this.setData({
            listspecid: i
          })
          break;
        }
      }
    } else if (this.data.product.specType1 && !this.data.product.specType2 && !this.data.product.specType3) {
      for (var i = 0; i < this.data.listSpec.length; i++) {
        if (this.data.listSpec[i].specDetails1 == this.data.item01) {
          this.setData({
            listspecid: i
          })
          break;
        }
      }
    }
    if (this.data.listspecid !== '') {
      this.setData({
        //真实规格
        RealListspec: this.data.listSpec[this.data.listspecid]
      })
      if (this.data.RealListspec.specDetails1 && this.data.RealListspec.specDetails2 && !this.data.RealListspec.specDetails3) {
        //动态改变选中规格
        // if (this.data.listspec1) {
        //   for (var i = 0; i < this.data.listspec1[this.data.product.specType1].length; i++) {
        //     if (this.data.RealListspec.specDetails1 == this.data.listspec1[this.data.product.specType1][i]) {
        //       this.setData({
        //         radioCheckVal01: i
        //       })
        //     }
        //   }
        // } else {
        //   for (var i = 0; i < this.data.listspecs[this.data.product.specType1].length; i++) {

        //   }
        // }
        // if (this.data.listspec2) {
        //   for (var i = 0; i < this.data.listspec2[this.data.product.specType2].length; i++) {
        //     if (this.data.RealListspec.specDetails2 == this.data.listspec2[this.data.product.specType2][i]) {
        //       this.setData({
        //         radioCheckVal02: i
        //       })
        //     }
        //   }
        // }
      } else if (this.data.RealListspec.specDetails1 && this.data.RealListspec.specDetails2 && this.data.RealListspec.specDetails3) {
        // if (this.data.listspec1) {
        //   for (var i = 0; i < this.data.listspec1[this.data.product.specType1].length; i++) {
        //     if (this.data.RealListspec.specDetails1 == this.data.listspec1[this.data.product.specType1][i]) {
        //       this.setData({
        //         radioCheckVal01: i
        //       })
        //     }
        //   }
        // }
        // if (this.data.listspec2) {
        //   for (var i = 0; i < this.data.listspec2[this.data.product.specType2].length; i++) {
        //     if (this.data.RealListspec.specDetails2 == this.data.listspec2[this.data.product.specType2][i]) {
        //       this.setData({
        //         radioCheckVal02: i
        //       })
        //     }
        //   }
        // }
        // if (this.data.listspec3) {
        //   for (var i = 0; i < this.data.listspec3[this.data.product.specType3].length; i++) {
        //     if (this.data.RealListspec.specDetails3 == this.data.listspec3[this.data.product.specType3][i]) {
        //       this.setData({
        //         radioCheckVal03: i
        //       })
        //     }
        //   }
        // }
      }

    }

    if (this.data.listspecid !== '') {
      if (this.data.RealListspec.specDetails1 && !this.data.RealListspec.specDetails2) {
        this.setData({
          count: 1,
          selectedSpec: this.data.product.specType1+':'+this.data.RealListspec.specDetails1
        })
      } else if (this.data.RealListspec.specDetails2 && !this.data.RealListspec.specDetails3) {
        this.setData({
          count: 1,
          selectedSpec: this.data.product.specType1+':'+this.data.RealListspec.specDetails1 + '   ' + this.data.product.specType2+':'+this.data.RealListspec.specDetails2
        })
      } else if (this.data.RealListspec.specDetails3) {
        this.setData({
          count: 1,
          selectedSpec: this.data.product.specType1 + ':' + this.data.RealListspec.specDetails1 + '   ' + this.data.product.specType2 + ':' + this.data.RealListspec.specDetails2 + '   ' + this.data.product.specType3+':'+this.data.RealListspec.specDetails3
        })
      }
    }
    //allPrice(this, this.data.listSpec[e.detail.value]);
    allPrice(this, this.data.product, this.data.listspecid);
  },

  /* *****************************选择规格end***************************** */



  /**
   * 进入下一步提交订单
   */
  next: function (e) {

    for (var i = 0; i < this.data.allgroupList.length; i++) {
      //如果此用户开过或参加过此团
      if (this.data.allgroupList[i].userId == this.data.userid) {
        this.setData({
          iscantuan: 1
        })
        break;
      }
    }
    if (this.data.iscantuan == 1) {
      this.showZanTopTips(this.prompt, '你已经开或参过此团，请进入我的->我的拼团进行查看');
    } else {
      if (this.data.product.specType1 && !this.data.product.specType2 && !this.data.product.specType3) {
        if (this.data.isClickSpecification1 == 1) {
          this.setData({
            showDialog: false,
            count: this.data.count,
            selectedSpec: this.data.selectedSpec
          });

          startPay(this);
        } else {

          this.showZanToast('请选择 ' + this.data.product.specType1);
        }
      } else if (this.data.product.specType2 && !this.data.product.specType3) {
        if (this.data.isClickSpecification1 == 1 && this.data.isClickSpecification2 == 1) {
          this.setData({
            showDialog: false,
            count: this.data.count,
            selectedSpec: this.data.selectedSpec
          });

          startPay(this);
        } else {
          if (this.data.isClickSpecification1 != 1) {
            this.showZanToast('请选择 ' + this.data.product.specType1);
          } else {
            this.showZanToast('请选择 ' + this.data.product.specType2);
          }


        }
      } else if (this.data.product.specType3) {
        if (this.data.isClickSpecification1 == 1 && this.data.isClickSpecification2 == 1 && this.data.isClickSpecification3 == 1) {
          this.setData({
            showDialog: false,
            count: this.data.count,
            selectedSpec: this.data.selectedSpec
          });

          startPay(this);
        } else {
          if (this.data.isClickSpecification1 != 1) {
            this.showZanToast('请选择 ' + this.data.product.specType1);
          } else if (this.data.isClickSpecification1 == 1 && this.data.isClickSpecification2 != 1) {
            this.showZanToast('请选择 ' + this.data.product.specType2);
          } else if (this.data.isClickSpecification1 == 1 && this.data.isClickSpecification2 == 1) {
            this.showZanToast('请选择 ' + this.data.product.specType3);
          }
        }
      } else if (!this.data.product.specType1 && !this.data.product.specType2 && !this.data.product.specType3) {
        this.setData({
          showDialog: false,
          count: this.data.count,
          selectedSpec: this.data.selectedSpec
        });

        startPay(this);
      }
    }


  },

  /**
   * 减商品数量
   */
  reduce: function (e) {
    if (this.data.count > 1) {
      this.data.count--;
      this.setData({
        count: this.data.count
      });
      allPrice(this, this.data.product, this.data.listspecid);
    }
  },
  /**
   * 加商品数量
   */
  add: function (e) {
    // 判断是否超过限购数量
    if (this.data.count < this.data.product.confineCount) {
      this.data.count++;
      this.setData({
        count: this.data.count
      });
      allPrice(this, this.data.product, this.data.listspecid);
    } else {
      this.showZanTopTips(0, '您已达到限购数量');
    }

  },

  /**
   * 如果用户已授权，并且用户昵称不为空，则直接弹框
   */
  // chooseSpan: function (e) {
  //   if (this.data.product.isVirtual == 0 && this.data.product.isActive == 1) {
  //     this.setData({
  //       showDialog: true,
  //     });
  //   } else {
  //     this.showZanTopTips('商品已下架');
  //   }
  // },

  chooseSpan: function (e) {
    //先判断此用户是否开过或参加过此团团
    // allgroupList userid
    for (var i = 0; i < this.data.allgroupList.length; i++) {
      //如果此用户开过或参加过此团
      if (this.data.allgroupList[i].userId == this.data.userid) {
        this.setData({
          iscantuan: 1
        })
        break;
      }
    }
    if (this.data.iscantuan == 1) {
      this.showZanTopTips(this.prompt, '你已经开或参过此团，请进入我的->我的拼团进行查看');
    } else {
      this.setData({
        showDialog: true,
      });
    }


  },

  formSubmit: function (e) {
    this.setData({
      formId: e.detail.formId
    })
  },

  getPhoneNumber: function (e) {
    if (e.detail.encryptedData) {
      network._post_json(this, api.wxphone, { encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: app.globalData.sessionKey }, bindSuccess);
    }
  },

  refresh(count) {
    this.setData({
      groupCount: this.data.groupCount - 1
    })
    if (parseInt(this.data.groupCount) <= 0) {
      this.setData({
        groupCount: 0
      })
    }

    var user = {
      avatarUrl: app.globalData.userAndTenantInfo.avatarUrl,
      nickName: app.globalData.userAndTenantInfo.nickName
    }
    this.data.spellGroup.user = user;
    this.setData({
      spellGroup: this.data.spellGroup
    })
  },
  gotoCantuan: function (e) {
    var tuanitem = e.currentTarget.dataset.tuanitem;
    this.setData({
      spellGroupsId: tuanitem.spellGroupsId,
      sgCombId: tuanitem.sgCombId,
      nowclock: tuanitem.clock,
      userSpellGroupId: tuanitem.id,
      originator: tuanitem.originator
    })
    network._post_json(this, api.spellGroupsActiveDetail, { sgCombId: this.data.sgCombId, spellGroupsId: this.data.spellGroupId, status: 0, originator: this.data.originator }, FSuccess);
  },
  //查看更多
  groupMore: function (e) {
    wx.navigateTo({
      url: '/pages/product/SpellGroupMore?spellGroupsId=' + this.data.spellGroupId
    })
  }
}))

/**
 * 计算所选商品的总价
 */
var allPrice = function (that, item, priceid) {
  if (item.spellGroupsCombs && item.spellGroupsCombs.length > 0) {
    if (priceid !== '' && priceid != undefined) {
      var spec = item.spellGroupsCombs[priceid].specValue2
    } else {
      var spec = item.sgPrice
    }
    that.setData({
      price: spec * that.data.count
    })
  } else {
    that.setData({
      price: item.sgPrice * that.data.count
    })
  }
}

// 获取拼团详情结果
var detailsSuccess = function (that, res) {
  if (app.globalData.userId) {
    that.setData({
      userid: app.globalData.userId
    })
  } else {
    // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
    // 所以此处加入 callback 以防止这种情况
    app.userAndTenantInfoCallback = res => {
      that.setData({
        userid: res.data.id
      })
    }
  }
  var arr = new Array();
  var listspecs = {};
  var initiallistspecs = {}
  /*
    最多有三个规格
  */
  if (res.data.data.specType1) {
    listspecs[res.data.data.specType1] = []
    for (var i = 0; i < res.data.data.spellGroupsCombs.length; i++) {
      listspecs[res.data.data.specType1].push(res.data.data.spellGroupsCombs[i].specDetails1)
    }
    listspecs[res.data.data.specType1] = utils.unique2(listspecs[res.data.data.specType1])
  }
  if (res.data.data.specType2) {
    listspecs[res.data.data.specType2] = [];
    initiallistspecs[res.data.data.specType2] = [];
    for (var i = 0; i < res.data.data.spellGroupsCombs.length; i++) {
      listspecs[res.data.data.specType2].push(res.data.data.spellGroupsCombs[i].specDetails2);
      //没有选择任何规格的时候，动态取出第二个规格
      if (res.data.data.spellGroupsCombs[i].specDetails1 == listspecs[res.data.data.specType1][0]) {
        initiallistspecs[res.data.data.specType2].push(res.data.data.spellGroupsCombs[i].specDetails2)
      }
    }
    listspecs[res.data.data.specType2] = utils.unique2(listspecs[res.data.data.specType2]);
    initiallistspecs[res.data.data.specType2] = utils.unique2(initiallistspecs[res.data.data.specType2]);
  }
  if (res.data.data.specType3) {
    listspecs[res.data.data.specType3] = [];
    initiallistspecs[res.data.data.specType3] = [];
    for (var i = 0; i < res.data.data.spellGroupsCombs.length; i++) {
      listspecs[res.data.data.specType3].push(res.data.data.spellGroupsCombs[i].specDetails3)
      //没有选择任何规格的时候，动态取出第三个规格
      if (res.data.data.spellGroupsCombs[i].specDetails1 == listspecs[res.data.data.specType1][0]) {
        initiallistspecs[res.data.data.specType3].push(res.data.data.spellGroupsCombs[i].specDetails3)
      }
    }
    listspecs[res.data.data.specType3] = utils.unique2(listspecs[res.data.data.specType3])
    initiallistspecs[res.data.data.specType3] = utils.unique2(initiallistspecs[res.data.data.specType3]);
  }

  if (res.data.data) {
    that.setData({
      product: res.data.data,
      listSpec: res.data.data.spellGroupsCombs,
      listspecs: listspecs,
      initiallistspecs: initiallistspecs
    })
    WxParse.wxParse('details', 'html', res.data.data.sgdetails, that, 0);
    // if (!that.data.listspecid) {
    //   if (res.data.data.specType1 && !res.data.data.specType2 && !res.data.data.specType3) {
    //     that.setData({
    //       count: 1,
    //       selectedSpec: res.data.data.spellGroupsCombs[0].specDetails1
    //     })
    //   } else if (res.data.data.specType2 && !res.data.data.specType3) {
    //     that.setData({
    //       count: 1,
    //       selectedSpec: res.data.data.spellGroupsCombs[0].specDetails1 + '+' + res.data.data.spellGroupsCombs[0].specDetails2
    //     })
    //   } else if (res.data.data.specType3) {
    //     that.setData({
    //       count: 1,
    //       selectedSpec: res.data.data.spellGroupsCombs[0].specDetails1 + '+' + res.data.data.spellGroupsCombs[0].specDetails2 + '+' + res.data.data.spellGroupsCombs[0].specDetails3
    //     })
    //   }
    // }
    if (that.data.product.spellGroupsCombs && !that.data.userSpellGroupId) {
      allPrice(that, that.data.product, that.data.listspecid);
    }

    //此商品团长列表
    network._post_json(that, api.getSpellGroupsDetail, { spellGroupsId: that.data.spellGroupId }, SDetailSuccess);
  }
}
var SDetailSuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      groupList: res.data.data.data,
      allgroupList: res.data.data.data1.reverse()
    })
  }
  countdown(that);
}
var startPay = function (that) {
  var product = {};
  var products = [];
  product.id = that.data.product.id;
  product.title = that.data.product.sgName;
  product.smallImgUrl = that.data.product.cutImg;
  product.conMode = that.data.product.conMode;
  product.count = that.data.count;
  if (that.data.userSpellGroupId && that.data.allgroupList.length>0) {
    //如果是用户参团(从分享里进入)
    product.userSpellGroupId = that.data.userSpellGroupId;
  }
  if (that.data.product.spellGroupsCombs && that.data.product.spellGroupsCombs.length > 0 && that.data.product.ruleType==2) {
    if (that.data.selectedSpec && (that.data.listspecid || that.data.listspecid == 0)) {
      product.price = that.data.RealListspec.specValue2;
      product.specId = that.data.product.spellGroupsCombs[that.data.listspecid].id;
      product.proprice = that.data.RealListspec.specValue1;
      product.specName = that.data.selectedSpec;
    } else if (that.data.selectedSpec && !that.data.listspecid && that.data.listspecid != 0) {
      product.price = that.data.product.sgPrice;
      product.proprice = that.data.product.proPrice;
      product.specId = that.data.product.spellGroupsCombs[0].id;
      product.specName = that.data.selectedSpec;
    }
  } else {
    product.price = that.data.product.sgPrice;
    product.proprice = that.data.product.proPrice;
  }


  products.push(product);
  try {
    wx.setStorageSync('spell_products', products)
  } catch (e) {
  }
  wx.navigateTo({
    url: '/pages/pay/SpellPay?spellGroupId=' + that.data.product.id
  })
}

var bindSuccess = function (that, res) {
  if (that.data.formId && app.globalData.userAndTenantInfo.mobile != res.data.data.mobile) {
    network._post_json(that, api.sendCoupn, { formId: that.data.formId, type: 2 }, sendCoupnSuccess);
  }

  app.globalData.userAndTenantInfo.mobile = res.data.data.mobile;
  that.setData({
    mobile: res.data.data.mobile
  })
}

var ruleSuccess = function (that, res) {
  that.setData({
    spellGroup: res.data.data[0],
    // price: res.data.data.proGroupPrice,
    groupCount: parseInt(res.data.data[0].groupSuccCount) - parseInt(res.data.data[0].groupCount)
  })
  if (parseInt(that.data.groupCount) <= 0) {
    that.setData({
      groupCount: 0
    })
  }
  allPrice(that, res.data.data[0]);
}

var fxcsSuccess = function (that, res) {
  that.showZanToast('分享成功');
}

var FSuccess = function (that, res) {
  if (res.data.data.specType1 && !res.data.data.specType2 && !res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1
  }
  if (res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2
  }
  if (res.data.data.specType3) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2 + '   ' + res.data.data.specType3 + ':' + res.data.data.spellGroupsComb.specDetails3
  }

  if (res.data.data) {
    that.setData({
      groupsellDetail: res.data.data,
      specDetails: res.data.data.spellGroupsComb ? res.data.data.spellGroupsComb.specDetails : '',
      cantuanlist: res.data.data.spellGroupsDetails
    })

  }
  if (that.data.nowclock != "已经截止") {
    wx.navigateTo({
      url: '/pages/product/SpellGroupDetails?spellGroupId=' + that.data.spellGroupId + '&userSpellGroupId=' + that.data.userSpellGroupId
    })
  }


}

var bind = function (options) {
  if (options.shared) {
    wx.request({
      url: api.bindUserFormQRcode,
      data: {
        openId: app.globalData.openId,
        userId: app.globalData.userId,
        tenantId: app.globalData.tenantId,
        distributorId: options.shared,
      },
      method: 'POST',
      success: function (res) {
      }
    });
  }
}

var sendCoupnSuccess = function (that, res) { }